package com.ptszyxx.popose.module.main.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.RankEnum;
import com.ptszyxx.popose.common.utils.YTabPagerUtil;
import com.ptszyxx.popose.databinding.FragmentHomeRankTabBinding;
import com.ptszyxx.popose.module.main.home.vm.HomeRankTabVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;

import java.util.ArrayList;

/**
 * 日榜，周榜，月榜
 */
public class HomeRankTabFragment extends BaseFragment<FragmentHomeRankTabBinding, HomeRankTabVM> {

    private String name;

    public HomeRankTabFragment(){}

    public HomeRankTabFragment(String name){
        this.name = name;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home_rank_tab;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public HomeRankTabVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(HomeRankTabVM.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        viewModel.rankEnum = RankEnum.get(name);
    }

    @Override
    public void initView() {
        super.initView();
        ArrayList<Fragment> mFragments = new ArrayList<>();
        String[] mTitles = getResources().getStringArray(R.array.rank_item_list);
        for (String item: mTitles) {
            mFragments.add(new HomeRankDataFragment(item, viewModel.rankEnum));
        }
        YTabPagerUtil.initSegment(this, binding.tabLayout, binding.viewPager, mTitles, mFragments);
    }
}
